package com.xjj.hash;

import java.util.HashSet;
import java.util.Set;

public class E0874WalkingRobotSimulation {

    public int robotSim(int[] commands, int[][] obstacles) {
        // 北
        int status = 0;
        // 北,东,南,西
        int[] x = {0, 1, 0, -1};
        int[] y = {1, 0, -1, 0};
        Set<String> obs = new HashSet<>();
        for (int[] arrs : obstacles) {
            obs.add(arrs[0] + "," + arrs[1]);
        }
        int currentX = 0;
        int currentY = 0;
        int max = 0;
        for (int command : commands) {
            if (command > 0) {
                for (int i = 0; i < command; i++) {
                    if (!obs.contains((currentX + x[status]) + "," + (currentY + y[status]))) {
                        currentX = currentX + x[status];
                        currentY = currentY + y[status];
                        max = Math.max(max, currentX * currentX + currentY * currentY);
                    }
                }
            } else if (command == -1) {
                status = (status + 1) % 4;
            } else if (command == -2) {
                status = (status - 1 + 4) % 4;
            }
        }
        return max;
    }

}
